package com.jiai.cloud.base.tools;


import com.jiai.cloud.base.entity.RestResult;
import com.jiai.cloud.base.util.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestResultGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestResultGenerator.class);

    /**
     * 封装返回体
     * @param success
     * @param code
     * @param data
     * @param message
     * @param description
     * @return
     */
    public static <T> RestResult<T> genResult(boolean success, int code, T data, String message, String description) {
        RestResult<T> result = RestResult.newInstance();
        result.setResult(success);
        result.setCode(code);
        result.setData(data);
        result.setDescription(description);
        result.setMessage(message);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("generate rest result:{}", result);
        }
        return result;
    }

    /**
     * success
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genSuccessResult(T data) {

        return genResult(true,200, data, null,null);
    }
    
    /**
     * success
     * @param message
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genSuccessResult(String message) {

        return genResult(true,200, null, message,null);
    }

    /**
     * error message
     * @param message error message
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genErrorResult(String message) {

        return genResult(false,500, null, message,null);
    }
    /**
     * 返回异常并自定义异常代码
     * @param code
     * @param message
     * @return
     */
    public static <T> RestResult<T> genErrorResult(int code,String message) {

        return genResult(false,code, null, message,null);
    }
    /**
     * 返回异常并给出开发者异常信息
     * @param message
     * @param description
     * @return
     */
    public static <T> RestResult<T> genErrorResult(String message,String description) {

        return genResult(false,500, null, message,description);
    }
    
    /**
     * 返回异常并给出开发者异常信息和自定义异常常代码
     * @param code
     * @param message
     * @param description
     * @return
     */
    public static <T> RestResult<T> genErrorResult(int code,String message,String description) {

        return genResult(false,code, null, message,description);
    }


    /**
     * error
     * @param error error enum
     * @param <T>
     * @return
     */
    public static <T> RestResult<T> genErrorResult(ErrorCode error) {
    	 return genErrorResult(error.getCode(),error.getMessage());
    }

    /**
     * 返回默认成功信息
     * @return
     */
    public static RestResult genSuccessResult() {
        return genSuccessResult("操作成功");
    }
}